<template>
  <a-form
    :model="generatorConfig"
    layout="horizontal"
    :label-col="{ span: 6 }"
    :wrapper-col="{ span: 18 }"
  >
    <!-- 基础配置 -->
    <div>
      <div class="module-title">基础配置</div>
      <a-row :gutter="16" class="row">
        <a-col :span="12">
          <a-form-item label="接口前缀">
            <a-input v-model:value="generatorConfig.apiPathPrefix" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="表名前缀">
            <a-input v-model:value="generatorConfig.tableNamePrefix" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="16" class="row">
        <a-col :span="12">
          <a-form-item label="基础包名">
            <a-input v-model:value="generatorConfig.basePackage" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="作者">
            <a-input v-model:value="generatorConfig.author" />
          </a-form-item>
        </a-col>
      </a-row>
    </div>

    <!-- 框架与工具 -->
    <div>
      <div class="module-title">框架与工具</div>
      <a-row :gutter="16" class="row">
        <a-col :span="12">
          <a-form-item label="ORM 框架">
            <a-select
              v-model:value="generatorConfig.ormType"
              placeholder="选择ORM框架"
              @change="handleOrmTypeChange"
            >
              <a-select-option value="mybatisPlus">MyBatisPlus</a-select-option>
              <a-select-option value="jpa">JPA</a-select-option>
              <a-select-option value="mybatis">MyBatis</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="12" v-if="filterTemplates.length > 0">
          <a-form-item label="代码模板">
            <a-select v-model:value="generatorConfig.templateId" placeholder="选择模板">
              <a-select-option v-for="template in filterTemplates" :value="template.id">
                {{ template.templateName }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="16" type="flex" class="row">
        <a-col :span="12">
          <a-form-item label="Lombok">
            <a-switch v-model:checked="generatorConfig.useLombok" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="Swagger">
            <a-switch v-model:checked="generatorConfig.swaggerSupport" />
          </a-form-item>
        </a-col>
      </a-row>
    </div>

    <!-- 注释配置 -->
    <div>
      <div class="module-title">注释配置</div>
      <a-row :gutter="16" type="flex" class="row">
        <a-col :span="6">
          <a-form-item label="类注释" :label-col="{ span: 8 }">
            <a-switch v-model:checked="generatorConfig.useClassComment" />
          </a-form-item>
        </a-col>
        <a-col :span="9" :label-col="{ span: 8 }">
          <a-form-item label="方法注释">
            <a-switch v-model:checked="generatorConfig.useMethodComment" />
          </a-form-item>
        </a-col>
        <a-col :span="9" :label-col="{ span: 8 }">
          <a-form-item label="字段注释">
            <a-switch v-model:checked="generatorConfig.useFieldComment" />
          </a-form-item>
        </a-col>
      </a-row>
    </div>

    <!-- 操作按钮 -->
    <div class="flex justify-end mt-4">
      <a-button @click="handleCancel" class="mr-2">取消</a-button>
      <a-button type="primary" @click="generateCode">生成</a-button>
    </div>
  </a-form>
</template>

<script setup>
import Http from '@/api/http'
import { useUserStore } from '@/stores/user'
import { onMounted, reactive, ref } from 'vue'

// 定义 props
const props = defineProps({
  handleCancel: {
    type: Function,
    required: true
  },
  handleGenerate: {
    type: Function,
    required: true
  }
})

// 生成器配置
let generatorConfig = reactive({
  //接口前缀
  apiPathPrefix: '/api',
  //去除表前缀
  tableNamePrefix: 't_',
  //包名
  basePackage: 'com.tool4j.ai',
  //作者
  author: '',
  //orm框架
  ormType: 'mybatisPlus',
  //是否使用lombok
  useLombok: true,
  //是否使用swagger
  swaggerSupport: false,
  //是否开启类注释
  useClassComment: true,
  //是否开启方法注释
  useMethodComment: true,
  //是否开启字段注释
  useFieldComment: true,
  //代码模板
  templateId: null,
  methodConfigMap: {
    findPage: {
      label: '分页查询',
      name: 'findPage',
      apiPath: '/findPage',
      methodType: 'POST',
      enabled: true
    },
    findList: {
      label: '列表查询',
      name: 'findList',
      apiPath: '/findList',
      methodType: 'POST',
      enabled: true
    },
    findById: {
      label: '查询详情',
      name: 'findById',
      apiPath: '/{id}',
      methodType: 'GET',
      enabled: true
    },
    save: {
      label: '新增',
      name: 'insert',
      apiPath: '',
      methodType: 'POST',
      enabled: true
    },
    update: {
      label: '修改',
      name: 'update',
      apiPath: '',
      methodType: 'PUT',
      enabled: true
    },
    delete: {
      label: '删除',
      name: 'delete',
      apiPath: '/{id}',
      methodType: 'DELETE',
      enabled: true
    }
  },
  generateType: 'full', // 生成方式：full-生成完整工程、mvc-生成MVC部分代码
  useDefaultPage: true, // 是否使用系统内置分页
  resultConfig: {} // 响应类配置
})

const templates = ref([])
const filterTemplates = ref([])
const userInfoStore = useUserStore()

function handleCancel() {
  props.handleCancel()
}

function generateCode() {
  userInfoStore.config = generatorConfig
  props.handleGenerate(generatorConfig)
}

function handleOrmTypeChange(value) {
  filterTemplates.value = templates.value.filter((item) => item.ormType === value)
  generatorConfig.templateId = null
}

function getTemplates() {
  Http.get('/api/templateInfo/findAll').then((res) => {
    templates.value = res.data
    filterTemplates.value = templates.value.filter((item) => item.ormType === 'mybatisPlus')
    generatorConfig.templateId = null
    if (userInfoStore && userInfoStore.config) {
      generatorConfig = userInfoStore.config
    }
  })
}

onMounted(() => {
  if (userInfoStore && userInfoStore.config) {
    generatorConfig = userInfoStore.config
  }
  getTemplates()
})
</script>

<style scoped>
.module-title {
  font-weight: 600;
  padding: 12px;
  border-bottom: 1px solid #f5f5f5;
  margin-bottom: 12px;
}

.row {
  height: 40px;
}

.mb-4 {
  margin-bottom: 6px;
}
.mt-4 {
  margin-top: 6px;
}
.mr-2 {
  margin-right: 8px;
}
.flex {
  display: flex;
}
.justify-end {
  justify-content: flex-end;
}
</style>
